﻿ 
$(document).ready(function () {


    var Detasumy = "";

    $.focusblur = function (focusid) {

        var focusblurid = $(focusid);

        focusblurid.click(function () {
            Detasumy = $(this).attr("id");
            NewDate();
            $(".fixeddate").css("display", "block");
            $(".fixedHiden").css("display", "block");
            $(".fixeddate").css("top", $(this).offset().top - $(".fixeddate").height() - 15);
        });
    };
    $(".Cancel").click(function () { $(".fixeddate").css("display", "none"); $(".fixedHiden").css("display", "none"); });
    $.focusblur(".SectDeta");

    $(".Add").click(function () {
        getdate($(this).attr("id"));
    });
    $(".comfirn").click(function () {
        $(".fixeddate").css("display", "none"); $(".fixedHiden").css("display", "none");
        if (Detasumy == "StartDate") {
            $("#StartYear").html($("#Year").val());
            $("#StartMoth").html($("#Moth").val());
            $("#StartDay").html($("#Day").val());
        }
        else {
            $("#EndYear").html($("#Year").val());
            $("#EndMoth").html($("#Moth").val());
            $("#EndDay").html($("#Day").val());
        }
    });
});
function NewDate() {
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth();
    var date = now.getDate();
    $("#Year").val(year);
    $("#Moth").val(month + 1);
    $("#Day").val(date);
    $("#sYear").html(year);
    $("#sMoth").html(month + 1);
    $("#sDay").html(date);
}
function getdate(id) {
    switch (id.toString()) {
        case "Yearadd": Yearadd(); break;
        case "YearCut": YearCut(); break;
        case "Mothadd": Mothadd(); break;
        case "MothCut": MothCut(); break;
        case "Dayadd": Dayadd(); break;
        case "DayCut": DayCut(); break;
    }
}
function Yearadd() {
    var yar = $("#Year").val();
    $("#Year").val(++yar);
    $("#sYear").html(yar);
}
function YearCut() {
    var yar = $("#Year").val();
    $("#Year").val(--yar);
    $("#sYear").html(yar);
}
function Mothadd() {
  var Month =  $("#Moth").val();
  if (Month >= 12) {
      Month = 0;
      Yearadd();
  }
  $("#Moth").val(++Month);
  $("#sMoth").html(Month);
  //当月的最大天数
  var yar = $("#Year").val();
  var days = daysInMonth(Month - 1, yar);
  if (days < $("#Day").val()) {
      $("#Day").val(days);
  }

 }
 function MothCut() {
     var Month = $("#Moth").val();
     if (Month <= 1) {
         Month = 13;
         YearCut();
     }
     $("#Moth").val(--Month);
     $("#sMoth").html(Month); 
      //当月的最大天数
     var yar = $("#Year").val();
     var days = daysInMonth(Month - 1, yar);
     if (days < $("#Day").val()) {
         $("#Day").val(days);
     }

 }
 function Dayadd() { 
     var moth=$("#Moth").val();
     var year = $("#Year").val();
     var day = $("#Day").val();
     var days = daysInMonth(moth - 1, year);
     if (day >= days) {
         day = 0;
         Mothadd();
     }
     $("#Day").val(++day);
     $("#sDay").html(day);

   
}
function DayCut() {
    var day = $("#Day").val();
    if (day <= 1) {
        MothCut();
        var year = $("#Year").val();
        var moth = $("#Moth").val();
        var days = daysInMonth(moth - 1, year);
        day = days + 1;
       
    }
    $("#Day").val(--day);
    $("#sDay").html(day);
}

function daysInMonth(iMonth, iYear)
{
    return 32 - new Date(iYear, iMonth, 32).getDate();
}

function isLeapYear() {//是否是闰年
    iYear = $("#Year").val();
    if (iYear % 4 == 0 && iYear % 100 != 0) {
        return true;
    } else {
        if (iYear % 400 == 0) {
            return true;
        } else {
            return false;
        }
    }
}